﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace MiniFox.Web
{
    /// <summary>
    /// 
    /// </summary>
    [StartOrdinal(1)]
    public class WebApplicationRunner : IWebApplicationRunner
    {
        /// <summary>
        /// 
        /// </summary>
        public WebApplicationRunner()
        {
            WebHostFactory = new DefaultWebHostFactory();
            this.CancellationToken = new CancellationToken();
        }
        /// <summary>
        /// 
        /// </summary>
        public virtual IWebHostFactory WebHostFactory
        {
            get;
        }
        /// <summary>
        /// 
        /// </summary>
        public CancellationToken CancellationToken { get; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        public void OnProcess(params string[] args)
        {
            var host = this.WebHostFactory.CreateWebHost(args);
            host.Run();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public async Task OnProcessAsync(params string[] args)
        {
            var host = this.WebHostFactory.CreateWebHost(args);
            await host.RunAsync(this.CancellationToken);
        }
    }
}
